package cn.com.egova.permission.szzt;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import cn.com.egova.permission.R;
import cn.com.egova.permission.szzt.lib.AndPermission;
import cn.com.egova.permission.szzt.lib.PermissionListener;

public class SZZTActivity extends Activity implements PermissionListener{
    private static final String TAG = "SZZTActivity";
    private Button btn_call;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_szzt);

        initData();

        initActionBar();

        initView();
    }

    private void initData() {
        title = getIntent().getStringExtra("title");
        if (TextUtils.isEmpty(title)) {
            title = "Permission";
        }
    }

    private void initActionBar() {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setTitle(title);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(false);
        }
    }

    private void initView() {
        btn_call = (Button) findViewById(R.id.btn_call);
        btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){  //23,Android 6.0
                    checkPermission();
                }else{
                    call();
                }
            }
        });
    }

    /**
     * 若已经有权限，直接执行拥有权限后回调，若没有，则申请权限
     */
    private void checkPermission() {
        if (PermissionUtil.checkPermissions(this,PermissionUtil.CALL_PHONE,PermissionUtil.TAKE_CAMERA)) {
            call();
        }
    }

    private void call() {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri uri = Uri.parse("tel:" + "10086");
            intent.setData(uri);
            startActivity(intent);
            Toast.makeText(this, "打电话啦", Toast.LENGTH_SHORT).show();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }



    //-----------------------------------------------------权限相关回调--------------------------------------------

    @Override
    public void onSucceed(int requestCode, List<String> grantPermissions) {
        switch (requestCode) {
            case PermissionUtil.REQUEST_CODE_PERMISSION_OTHER:
                call();
                break;
            default:
                break;
        }
    }


    @Override
    public void onFailed(int requestCode, final List<String> deniedPermissions) {
        switch (requestCode) {
            case PermissionUtil.REQUEST_CODE_PERMISSION_OTHER: {
                Toast.makeText(this, R.string.permission_message_fail, Toast.LENGTH_SHORT).show();
                break;
            }
        }

        //权限失败设置
        PermissionUtil.afterFailPermission(this, requestCode, deniedPermissions, new Runnable() {
            @Override
            public void run() {
                Toast.makeText(SZZTActivity.this, R.string.permission_message_fail, Toast.LENGTH_SHORT).show();
            }
        });
    }


    //下面这个方法是Activity的，但是一定要重写。
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
            grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        /**
         * 转给AndPermission分析结果。非常重要的步骤，否则即使授权成功，也不会自动执行onSucceed()里的内容。
         *
         * @param requestCode  请求码。
         * @param permissions  权限数组，一个或者多个。
         * @param grantResults 请求结果。
         * @param listener PermissionListener 对象。
         */
        AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}
